{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 注意！请根据提示操作！\n",
    "\n",
    "如果当页面加载完成，你还是看到了这条提示\n",
    "\n",
    "那么你的notebook并未被信任，导致代码无法正常运行\n",
    "\n",
    "点击右上角的`不可信`，将本notebook列入可信列表，即可正常运行。\n",
    "\n",
    "![可信按钮](./mgr/trust.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 判断\n",
    "\n",
    "符号解释:\n",
    "\n",
    "```\n",
    "大于: >\n",
    "小于: <\n",
    "等于: == (注意在Python语言中，判断两者是否相等使用的是==符号，单个=符号仅代表赋值功能。)\n",
    "大于等于: >=\n",
    "小于等于: <=\n",
    "不等于: !=\n",
    "并且(与): and\n",
    "或者(或): or\n",
    "否(非): not\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2\n",
    "b = 1\n",
    "if a > b:\n",
    "    print('a比b大')  # Python中用缩进（1个缩进官方建议开头4个空格）来作为代码从属于什么逻辑之后的判断，例如此行，只有在a>b时程序才会正常运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2\n",
    "b = 3\n",
    "if a > b:\n",
    "    print('a比b大')  # 此时这行就不会运行，因为a并不比b大，条件不满足"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2\n",
    "b = 1\n",
    "if a > b:\n",
    "print('a比b大')  # 程序会报错，因为if判断的冒号之后，没有任何缩进的语句，系统将不知道在判断成功后做什么事情"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2\n",
    "b = 1\n",
    "if a > b:\n",
    "    print('a比b大')  # 当a比b大时会运行\n",
    "print('比较完成')  # 由于该行没有缩进，所以该行的逻辑与上述的if判断无关，无论a、b大小如何都会运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2\n",
    "b = 3\n",
    "if a > b:\n",
    "    print('a比b大')\n",
    "print('比较完成')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2\n",
    "b = 3\n",
    "if a < b:\n",
    "    print('a比b小')\n",
    "print('比较完成')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2\n",
    "b = 2\n",
    "if a == b:\n",
    "    print('a和b相等')\n",
    "print('比较完成')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "本质上，if后面的判断，是在进行一个计算，计算的结果是真（True）和假（False），如果是真就运行冒号之后缩进的代码，如果是假就不运行之后缩进的代码。\n",
    "\n",
    "if可以和else配合，来方便书写，例如，0-59分为不合格，60-79分为合格，80-89为良好，90-100为优秀，如果单纯用if，会写成如下这样："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 100\n",
    "if a <= 59 and a >= 0:\n",
    "    print('不合格')\n",
    "if a <= 79 and a >= 60:\n",
    "    print('合格')\n",
    "if a <= 89 and a >= 80:\n",
    "    print('良好')\n",
    "if a <= 100 and a >= 90:\n",
    "    print('优秀')\n",
    "if not (a >= 0 and a <= 100):\n",
    "    print('错误：a的数值未在0-100之间')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果配合else和else if(elif)，可以写成如下代码:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 100\n",
    "if not (a >= 0 and a <= 100):\n",
    "    print('错误：a的数值未在0-100之间')\n",
    "elif a <= 59:\n",
    "    print('不合格')\n",
    "elif a <= 79:\n",
    "    print('合格')\n",
    "elif a <= 89:\n",
    "    print('良好')\n",
    "else:\n",
    "    print('优秀')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以自修修改上述a的值，来观察结果。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 关于缩进\n",
    "\n",
    "python中需要依靠缩进来判断哪些代码“从属于”哪些代码，为了理解缩进的作用，我们假设不用缩进来写代码，例如如下没有缩进的代码：\n",
    "\n",
    "```python\n",
    "if score < 60:\n",
    "print('不及格')\n",
    "print('请再接再厉！')  #如果分数小于60，这条是运行还是不运行呢？\n",
    "else:\n",
    "print('及格')\n",
    "print('程序运行完毕！')  # 这条代码到底是score不小于60的时候运行呢？还是程序完全运行完后运行？\n",
    "```\n",
    "\n",
    "观察上面的代码，缩进的作用就是告诉系统，哪些代码从属于哪些代码：\n",
    "\n",
    "```python\n",
    "if score < 60:\n",
    "    print('不及格')\n",
    "    print('请再接再厉！')  # 可以确定下这句话是小于60分时才运行\n",
    "else:\n",
    "    print('及格')\n",
    "print('程序运行完毕！')  # 可以确定这句话在执行完程序后无轮什么情况都能运行\n",
    "```\n",
    "\n",
    "所以冒号加缩进，就是告诉系统，哪些代码从属于哪些代码。\n",
    "\n",
    "一般在Python中，以4个空格为一个缩进。\n",
    "\n",
    "缩进可以叠加，例如：\n",
    "\n",
    "```python\n",
    "if score < 60:\n",
    "    if age < 18:\n",
    "        print('不及格，加油')  # 对待祖国花朵比较温柔\n",
    "    else:\n",
    "        print('怎么搞的！')  # 对待成年人毫不留情\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<hr>\n",
       "<link rel=\"stylesheet\" href=\"//cdn.bootcss.com/mdui/0.4.3/css/mdui.min.css\">\n",
       "<script src=\"./mgr/submit.js\"></script>\n",
       "<script src=\"./mgr/hide.js\"></script>\n",
       "<div class=\"mdui-container-fluid\">\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-500\" onclick=\"next_page(-1)\">上一章</button>\n",
       "    </div>\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-700\" onclick=\"window.open('/tree')\">目录</button>\n",
       "    </div>\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-900\" onclick=\"next_page(1)\">下一章</button>\n",
       "    </div>\n",
       "</div>\n",
       "<div id=\"html_output\">\n",
       "</div>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<hr>\n",
    "<link rel=\"stylesheet\" href=\"//cdn.bootcss.com/mdui/0.4.3/css/mdui.min.css\">\n",
    "<script src=\"./mgr/submit.js\"></script>\n",
    "<script src=\"./mgr/hide.js\"></script>\n",
    "<div class=\"mdui-container-fluid\">\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-500\" onclick=\"next_page(-1)\">上一章</button>\n",
    "    </div>\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-700\" onclick=\"window.open('/tree')\">目录</button>\n",
    "    </div>\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-900\" onclick=\"next_page(1)\">下一章</button>\n",
    "    </div>\n",
    "</div>\n",
    "<div id=\"html_output\">\n",
    "</div>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
